home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Sample Code / Snippets / QuickDraw / ColorizePict / PictInfoTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-28  |  9.4 KB  |  433 lines  |  [TEXT/KAHL]

  1. // buffered window shell App.  This file creates a window and attaches a GWorld to it, created
  2. // from information contained in the source picture.  You can open a picture and display it
  3. // in the window.  This is a shell for testing developer questions about offscreens.
  4. //
  5. // There are two good references for the offscreen stuff.  Inside Macintosh: Imaging with Quickdraw
  6. // and Scott Knaster & Keith Rollin'sbook: Macintosh Programming Secrets.  Both of these are available from 
  7. // Addison Wesley.
  8. //
  9. // Nick Thompson - June 6th 1994
  10.  
  11. /* Constant Declarations */
  12. #include <menus.h>
  13. #include <PictUtil.h>
  14. #include <QDOffScreen.h>
  15.  
  16.  
  17. #define    WWIDTH        470
  18. #define    WHEIGHT        330
  19.  
  20. #define WLEFT        (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  21. #define WTOP        (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  22.  
  23. #define HiWrd(aLong)    (((aLong) >> 16) & 0xFFFF)
  24. #define LoWrd(aLong)    ((aLong) & 0xFFFF)
  25.  
  26.  
  27.  
  28. enum {
  29.     mApple = 128,
  30.     mFile,
  31.     mEffects
  32. } ;
  33.  
  34. enum {
  35.     iAbout = 1
  36. } ;
  37. enum {
  38.     iOpen = 1,
  39.     iClose,
  40.     iUnused1,
  41.     iQuit = 4
  42. } ;
  43.  
  44. enum {
  45.     iUsePictPalette = 1
  46. } ;
  47.  
  48. static Boolean gQuitFlag = false ;
  49. static Point gStaggerPos = {50,50} ;
  50. static Boolean gUsePictPalette = true ;
  51.  
  52. // function prototypes
  53.  
  54. void InitToolbox( void ) ;
  55. void MainEventLoop( void ) ;
  56. void HandleKeyPress(EventRecord *event) ;
  57. void HandleOSEvent(EventRecord *event) ;
  58. void HandleMenuCommand(long menuResult) ;
  59. PicHandle DoReadPICT( short theRef, OSErr *theErr ) ;
  60. OSErr DoCreateWindow( PicHandle thePicture ) ;
  61. void AdjustMenus( void ) ;
  62.  
  63.  
  64. const RGBColor    kRGBBlack = {0, 0, 0};
  65. const RGBColor    kRGBWhite = {0xFFFF, 0xFFFF, 0xFFFF};
  66.  
  67.  
  68. main()
  69. {
  70.     InitToolbox() ;
  71.     
  72.     MainEventLoop();
  73. }
  74.  
  75.  
  76.  
  77. void InitToolbox()
  78. {
  79.     OSErr         retCode;
  80.     long         gestResponse;
  81.     Handle        menuBar = nil;
  82.     EventRecord event;
  83.     short        count;
  84.  
  85.  
  86.     InitGraf((Ptr) &qd.thePort);
  87.     InitFonts();
  88.     InitWindows();
  89.     InitMenus();
  90.     TEInit();
  91.     InitDialogs((long)nil);
  92.     InitCursor();
  93.  
  94.     // initialize application globals
  95.     
  96.     gQuitFlag = false;
  97.     
  98.     
  99.     menuBar = GetNewMBar(128);                // Read menus into menu bar, MBAR res id is 128
  100.     
  101.     if ( menuBar == nil )
  102.          ExitToShell();                        // if we dont have it then quit - your app 
  103.                                              // needs a dialog here
  104.  
  105.     SetMenuBar(menuBar);                    // Install menus
  106.     DisposHandle(menuBar);
  107.     
  108.     AddResMenu(GetMHandle(mApple), 'DRVR');    // Add DA names to Apple menu, ID 128
  109.  
  110.     DrawMenuBar();
  111. }
  112.  
  113.  
  114. void MainEventLoop()
  115. {
  116.     EventRecord     event;
  117.     WindowPtr       window;
  118.     short           thePart;
  119.     Rect            screenRect;
  120.     Point            aPoint = {100, 100};
  121.     GWorldPtr        theNewWorld ;
  122.     PixMapHandle    offPixMap ;
  123.     GrafPtr            oldPort ;
  124.  
  125.     while( !gQuitFlag )
  126.     {
  127.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  128.         {
  129.             AdjustMenus() ;
  130.  
  131.             switch (event.what) {
  132.                 case mouseDown:
  133.                 
  134.                     thePart = FindWindow( event.where, &window );
  135.                     
  136.                     switch( thePart ) {
  137.                         case inMenuBar: 
  138.                             HandleMenuCommand(MenuSelect(event.where));
  139.                             break;
  140.                         
  141.                         case inDrag:
  142.                     
  143.                             screenRect = (**GetGrayRgn()).rgnBBox;
  144.                             DragWindow( window, event.where, &screenRect );
  145.                             break ;
  146.                     
  147.                         case inContent:
  148.                     
  149.                             if (window != FrontWindow())
  150.                                 SelectWindow( window );
  151.                             break ;
  152.                     
  153.                         case inGoAway:
  154.                             if (TrackGoAway( window, event.where )) {
  155.                                 DisposeWindow ( window );
  156.                             }
  157.                             break ;
  158.                             
  159.                         default:
  160.                             break ;
  161.                     }
  162.                     break ;
  163.                             
  164.                         
  165.                 case updateEvt:
  166.                 
  167.                     window = (WindowPtr)event.message;
  168.                     GetPort(&oldPort ) ;    
  169.                     SetPort( window );
  170.                     
  171.                     BeginUpdate( window );
  172.                     
  173.                     // get the GWorld from the window refcon
  174.                     theNewWorld = (GWorldPtr)GetWRefCon ( window );
  175.                     offPixMap = GetGWorldPixMap( theNewWorld ) ;
  176.                     (void) LockPixels( offPixMap ) ;
  177.                     CopyBits( &((GrafPtr)theNewWorld)->portBits,
  178.                               &window->portBits,
  179.                               &window->portRect,
  180.                               &window->portRect,
  181.                               srcCopy,
  182.                               nil ) ;
  183.                     (void) UnlockPixels( offPixMap ) ;
  184.  
  185.                     EndUpdate( window );
  186.                     SetPort( oldPort ) ;
  187.                     break ;
  188.                     
  189.                 case keyDown:
  190.                 case autoKey:
  191.                     HandleKeyPress(&event);
  192.                     break;
  193.                     
  194.                 case diskEvt:
  195.                     if ( HiWrd(event.message) != noErr ) 
  196.                         (void) DIBadMount(aPoint, event.message);
  197.                     break;
  198.                     
  199.                 case osEvt:
  200.                 case activateEvt:
  201.                     break;
  202.  
  203.  
  204.             }
  205.         }
  206.     }
  207. }
  208.  
  209.  
  210. void HandleKeyPress(EventRecord *event)
  211. {
  212.     char    key;
  213.  
  214.     key = event->message & charCodeMask;
  215.     
  216.     // just check to see if we want to quit...
  217.     
  218.     if ( event->modifiers & cmdKey ) {        /* Command key down? */
  219.         HandleMenuCommand(MenuKey(key));
  220.     } 
  221. }
  222.  
  223.  
  224. void HandleMenuCommand(long menuResult)
  225. {
  226.     short        menuID;
  227.     short        menuItem;
  228.     Str255        daName;
  229.     DialogPtr    theDialog ; 
  230.     short        itemHit ;
  231.     SFTypeList    myTypes = { 'PICT' } ;
  232.     FSSpec        theFSSpec ;
  233.     PicHandle    thePicture ;
  234.     OSErr        err ;
  235.     short        theRef ;
  236.     
  237.     StandardFileReply    theSFReply ;
  238.  
  239.     menuID = HiWrd(menuResult);
  240.     menuItem = LoWrd(menuResult);
  241.     switch ( menuID ) {
  242.         case mApple:
  243.             switch ( menuItem ) {
  244.                 ModalFilterUPP         theProc ;
  245.  
  246.                 case iAbout:
  247.                 
  248.                     theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  249.                     
  250.                     // these two lil' snappers are system 7 only
  251.                     // so if you use them, check before!!
  252.                     GetStdFilterProc( &theProc ) ;
  253.                     SetDialogDefaultItem(theDialog, ok) ;
  254.                     
  255.                     
  256.                     do {
  257.                         ModalDialog ( theProc, &itemHit );
  258.                     } while( itemHit != ok ) ;
  259.                     DisposDialog ( theDialog );
  260.                     break;
  261.                     
  262.                 default:
  263.                     GetItem(GetMHandle(mApple), menuItem, daName);
  264.                     (void) OpenDeskAcc(daName);
  265.                     break;
  266.             }
  267.             break;
  268.         case mFile:
  269.             switch ( menuItem ) {
  270.                 case iOpen:
  271.                     // Get the file name to open
  272.                     StandardGetFile( nil, 1, myTypes, &theSFReply ) ;
  273.                     
  274.                     // did the user cancel?
  275.                     if(!theSFReply.sfGood)
  276.                         break ;
  277.                     
  278.                     // open the file
  279.                     err = FSpOpenDF( &theSFReply.sfFile, fsRdPerm, &theRef ) ;
  280.                     
  281.                     if( err != noErr )
  282.                         break ;     // should handle this properly
  283.                         
  284.                     thePicture = DoReadPICT( theRef, &err ) ;
  285.                     
  286.                     if( err != noErr )
  287.                         break ;     // should handle this properly
  288.                 
  289.                     // display the contents
  290.                     err = DoCreateWindow( thePicture ) ;
  291.                     
  292.                     break ;
  293.                     
  294.                 case iClose:
  295.                     DisposeWindow ( FrontWindow() );
  296.                     break ;
  297.                 case iQuit:
  298.                     gQuitFlag = true;
  299.                     break;
  300.             }
  301.             break;
  302.             
  303.             
  304.         case 131:
  305.             if( menuItem == 1 )
  306.                 if( FrontWindow() != nil )
  307.                     ColorizeImage( FrontWindow() ) ;
  308.             break; 
  309.  
  310.     }
  311.     HiliteMenu(0);        // Unhighlight whatever MenuSelect or MenuKey hilited
  312. }
  313.  
  314. void AdjustMenus( void ) 
  315. {
  316.     WindowPtr    theWindow ;
  317.     theWindow = FrontWindow() ;
  318.     if( theWindow != nil ) {
  319.         EnableItem ( GetMHandle ( mFile ), iClose );
  320.     }
  321.     else {
  322.         DisableItem ( GetMHandle ( mFile ), iClose );
  323.     }
  324. }
  325.  
  326. PicHandle DoReadPICT( short theRef, OSErr *theErr ) 
  327. {
  328.     long        theFileSize ;
  329.     PicHandle    thePicture ;
  330.     
  331.     // pict files have a 512 byte header at the front - we dont care about this
  332.     // we can find the size of the pict by subtracting 512 bytes from the length
  333.     // of the file.  We then want to resize the handle to that and read the data
  334.     // into the resized handle.
  335.     
  336.     if(( *theErr = GetEOF( theRef, &theFileSize )) != noErr ) {
  337.         FSClose( theRef ) ;
  338.         return nil ; 
  339.     }
  340.     
  341.     if(( *theErr = SetFPos( theRef, fsFromStart, 512)) != noErr ) {
  342.         FSClose( theRef ) ;
  343.         return nil ; 
  344.     }
  345.  
  346.     theFileSize -= 512 ;
  347.     
  348.     thePicture = (PicHandle)NewHandle( theFileSize ) ;
  349.     if( thePicture == nil ) {
  350.         FSClose( theRef ) ;
  351.         *theErr = MemError() ;
  352.         return nil ;         // what ever the mem manager error was
  353.     }
  354.     
  355.     HLock( (Handle)thePicture ) ;
  356.     *theErr = FSRead( theRef, &theFileSize, (Ptr)*thePicture ) ;
  357.     HUnlock(  (Handle)thePicture ) ;
  358.     
  359.     if( *theErr != noErr ) {
  360.         FSClose( theRef ) ;
  361.         return nil ; 
  362.     }
  363.  
  364.     return thePicture ;    
  365. }
  366.  
  367. OSErr DoCreateWindow( PicHandle thePicture )
  368. {
  369.  
  370.     Rect        theRect ;
  371.     OSErr        theErr ;
  372.     GWorldPtr    theNewWorld ;
  373.     CGrafPtr    savedPort ;
  374.     GWorldPtr    savedGWorld ;
  375.     WindowPtr    theWindow ;
  376.     GDHandle    oldDevice ;
  377.     
  378.     PictInfo        thePictInfo ;
  379.     PaletteHandle    thePictPalette = nil ;
  380.     CTabHandle        thePictCTab = nil ;
  381.     
  382.     // make an offscreen environment and image the pict into this
  383.     // Make a window the size of the pict
  384.     // store a reference to the GWorld in the Refcon of the window
  385.     // invalidate the window content area.
  386.     
  387.     theRect.top = (**thePicture).picFrame.top ;
  388.     theRect.left = (**thePicture).picFrame.left ;
  389.     theRect.bottom = (**thePicture).picFrame.bottom ;
  390.     theRect.right = (**thePicture).picFrame.right ;
  391.     
  392.     // create a gWorld 8 bits deep
  393.     
  394.     theErr = NewGWorld( &theNewWorld, 8, &theRect, thePictCTab, nil, 0L ) ;
  395.     
  396.     if( theErr != noErr ) 
  397.         return theErr ;
  398.     
  399.     // save the world
  400.     GetGWorld( &savedPort, &oldDevice ) ;
  401.     SetGWorld( theNewWorld, nil ) ;
  402.     
  403.     
  404.     RGBForeColor( &kRGBBlack ) ;        // ensure the fg and bg colors are 
  405.     RGBBackColor( &kRGBWhite ) ;        // as anticipated
  406.     
  407.     EraseRect( &theRect ) ;                // clear the area for the pict
  408.     PenMode( srcCopy ) ;                // ensure the t/f mode is as expected
  409.  
  410.     // render the image into the offscreen buffer
  411.     DrawPicture( thePicture, &theRect ) ;
  412.     
  413.     SetGWorld( savedPort, oldDevice ) ;
  414.     
  415.     // create the window
  416.     OffsetRect( &theRect, gStaggerPos.h, gStaggerPos.v) ;
  417.     gStaggerPos.h += 16 ;
  418.     gStaggerPos.v += 16 ;        // heh - should roll these around, but you wont 
  419.                                 // create more than a couple of windows, will you  :-)
  420.                                  
  421.     theWindow  = NewCWindow( nil, &theRect, "\pplayTime", true, 
  422.                                 documentProc, (WindowPtr)-1, true, (long)theNewWorld );    
  423.                     
  424.     // make sure it is visible
  425.     ShowWindow( theWindow ) ;
  426.     
  427.     SetGWorld( (CGrafPtr)theWindow, nil ) ;
  428.     
  429.     // invalidate the content region of the window - we don't do any drawing to it here.
  430.     InvalRect ( &theRect );
  431.     
  432.     SetGWorld( savedPort, oldDevice ) ;
  433. }